package edu.whu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.whu.demo.domain.ActivityDTO;
import edu.whu.demo.domain.ActivityMember;
import edu.whu.demo.domain.Entry;
import edu.whu.demo.dao.EntryDao;
import edu.whu.demo.domain.User;
import edu.whu.demo.exception.ActivityMemberException;
import edu.whu.demo.exception.EntryException;
import edu.whu.demo.service.IActivityMemberService;
import edu.whu.demo.service.IEntryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2022-12-06
 */
@Service
public class EntryServiceImpl extends ServiceImpl<EntryDao, Entry> implements IEntryService {

    @Autowired
    IActivityMemberService activityMemberService;

    //根据活动的id查找到uid，再查找到用户信息，返回分页信息
    @Override
    public IPage<User> getMembers(Long activityId,
                                  String keyword,
                                  Integer pageNum,
                                  Integer pageSize)
    {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("e.activity_id", activityId);
        if (keyword != null)
        {
            qw.like("u.name", keyword);
        }
        Page<User> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getMembers(page, qw);
    }

    public IPage<ActivityDTO> getActivityByEntry(Map<String, Object> condition,
                                              Integer pageNum,
                                              Integer pageSize)
    {
        Page<ActivityDTO> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ActivityDTO> qw = new QueryWrapper<>();
        qw.eq(condition.containsKey("uid"),"e.uid",condition.get("uid"));
        qw.eq(condition.containsKey("type"),"a.type",condition.get("type"));
        //可按"added_times"或者"begin_time"排序，按begin_time排序的有待商榷
        if (condition.containsKey("orderBy"))
        {
            //condition.get("order")为null时为升序
            qw.orderBy(true, !"Desc".equals(condition.get("order")),(String)condition.get("orderBy"));
        }
        qw.and(condition.containsKey("keyword"),
                wrapper->wrapper.like("a.name",condition.get("keyword"))
                .or().like(condition.containsKey("keyword"),"a.place",condition.get("keyword"))
                .or().like(condition.containsKey("keyword"),"a.info",condition.get("keyword"))
                .or().like(condition.containsKey("keyword"),"a.content",condition.get("keyword")));
        return this.baseMapper.getActivityByEntry(page,qw);
    }

    @Override
    public void delEntry(Entry entry) throws EntryException, ActivityMemberException {
        QueryWrapper<Entry> qw = new QueryWrapper<>();
        qw.eq("uid", entry.getUid())
                .eq("activity_id",entry.getActivityId());
        try {
            this.baseMapper.delete(qw);
        }
        catch (Exception e)
        {
            throw new EntryException("entry删除失败："+e.getMessage());
        }
        ActivityMember activityMember = new ActivityMember();
        activityMember.setUid(entry.getUid());
        activityMember.setActivityId(entry.getActivityId());
        activityMemberService.deleteSign(activityMember);
    }


}
